package com.hdhere.application.controller.admin;

import com.hdhere.application.service.BasicDataService;
import com.hdhere.application.service.VideoPostService;
import com.hdhere.domain.common.Pagination;
import com.hdhere.domain.common.rest.CallResult;
import com.hdhere.domain.media.VideoPost;
import com.hdhere.domain.media.page.VideoPostPagination;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;
import java.util.UUID;

/*******************************************************************************************
 * 类描述：  
 *
 * @author: dustin
 * @date: 2019-03-09 10:26
 * @Description:
 * @version 1.0
 *
 *
 * Version    Date       ModifiedBy                 Content  
 * -------- ---------    ----------         ------------------------  
 * 1.0      2019/3/9       dustin                               
 *******************************************************************************************
 */
@Api(tags = "视频发布")
@Controller
@RequestMapping("/admin/videoPost")
public class VideoPostController {

    @Value("${server.upload-path}")
    private String uploadPath;

    @Autowired
    private VideoPostService videoPostService;

    @Autowired
    private BasicDataService basicDataService;

    @GetMapping
    public String list(Map<String, Object> map){
        //发布状态数据
        var rowStateList = basicDataService.getEnumList("RowState");
        map.put("states", rowStateList);

        return "admin/videoPostList";
    }

    @ApiOperation(value = "分页列表")
    @PostMapping
    @ResponseBody
    public Pagination getPage(VideoPostPagination input){
        input.matchPage();
        Pagination page = videoPostService.getPage(input);
        return page;
    }

    @GetMapping({"/save/{id}","/save"})
    public String save(@PathVariable(required = false) Long id, @RequestParam(required = false) String videoId, Map<String, Object> map){

        var videoPost = id == null ? new VideoPost()
                : videoPostService.getOne(id);

        if(StringUtils.isNotBlank(videoId)) {
            videoPost.setVideoId(videoId);
            // TODO: 2019/4/6 优化 根据videoId带出分类数据
        }
        map.put("m", videoPost);

        //发布状态数据
        var rowStateList = basicDataService.getEnumList("RowState");
        map.put("states", rowStateList);

        return "admin/videoPostSave";
    }
    @ApiOperation(value = "保存")
    @PostMapping(value = "/save", produces = {"text/html; charset=UTF-8"})
    @ResponseBody
    public String Save(VideoPost post, MultipartFile file) throws Exception{

        if(file != null) {
            File dir = new File(uploadPath);
            if (!dir.exists()) dir.mkdirs();
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            //上传文件名
            String filename = UUID.randomUUID() + suffix;
            //服务器端保存的文件对象
            File serverFile = new File(uploadPath + filename);
            //将上传的文件写入到服务器端文件内
            file.transferTo(serverFile);

            String thumbnail = StringUtils.isBlank(post.getThumbnail()) ? filename : post.getThumbnail()+","+filename;
            post.setThumbnail(thumbnail);
        }
        videoPostService.save(post);

        return "<script>parent.$lv.toast('保存成功！');parent.tb.fnDraw();parent.layer.close(parent.layer.getFrameIndex(window.name));</script>";
    }


    @ApiOperation(value = "删除发布，取消视频发布状态")
    @PostMapping(value = "/del", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public CallResult delete(@RequestParam Long id){
        videoPostService.delete(id);
        return new CallResult(id);
    }

    @ApiOperation(value = "删除缩略图")
    @PostMapping(value = "/del/{id}/{file}", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public CallResult deleteThumbnail(@PathVariable Long id, @PathVariable String file){

        videoPostService.deleteThumbnail(id, file);
        return CallResult.ok();
    }
}
